package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PathDashPathEffect
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* PathEffectView03
* */
class PathEffectView03 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private var mPathEffect: PathDashPathEffect? = null
    
    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL_AND_STROKE
            color = context.resources.getColor(R.color.color_EF296F)
        }
    }

    private val mPath0 by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            translate(cx, cy)

            if (mPathEffect == null) {
                mPath0.apply {
                    moveTo(0F, -cy * 7 / 25)
                    lineTo(-cx * 7 / 25, 0F)
                    lineTo(cx * 7 / 25, 0F)
                    close()

                    /*val ratio = 3 / 25.0F
                    moveTo(-cx * ratio, -cy * ratio)
                    lineTo(cx * ratio, -cy * ratio)
                    lineTo(cx * ratio, cy * ratio)
                    lineTo(-cx * ratio, cy * ratio)
                    close()*/
                }
                val advance = (Math.PI * (cx * 7 / 12) * 2 / 9).toFloat()
                val phase = (Math.PI * (cx * 7 / 12) * 2 / 9).toFloat()
                //mPathEffect = PathDashPathEffect(mPath0, advance, phase, PathDashPathEffect.Style.TRANSLATE)
                mPathEffect = PathDashPathEffect(mPath0, advance, phase, PathDashPathEffect.Style.ROTATE)
                //mPathEffect = PathDashPathEffect(mPath0, advance, phase, PathDashPathEffect.Style.MORPH)
            }
            mPaint0.pathEffect = mPathEffect

            val baseX = cx * 7 / 12
            val baseY = cy * 7 / 12
            drawArc(
                -baseX,
                -baseY,
                baseX,
                baseY,
                0F,
                360F,
                false,
                mPaint0
            )
        }
    }
}